package com.ruoyi.web.controller.teachingmaterial;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.domain.LhqTeachingMaterial;
import com.ruoyi.domain.LhqTeachingMaterialGrade;
import com.ruoyi.domain.YcPersonal;
import com.ruoyi.domain.YcTask;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.service.ILhqTeachingMaterialGradeService;
import com.ruoyi.service.ILhqTeachingMaterialService;
import com.ruoyi.service.IYcPersonalService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 教材Controller
 * 
 * @author ruoyi
 * @date 2020-09-08
 */
@Controller
@RequestMapping("/ruoyi/material")
public class LhqTeachingMaterialController extends BaseController
{
    private String prefix = "ruoyi/material";
    private final static String ALL="全部";
    @Autowired
    private ILhqTeachingMaterialService lhqTeachingMaterialService;
    @Resource
    private ILhqTeachingMaterialGradeService iLhqTeachingMaterialGradeService;
    @Resource
    private IYcPersonalService iYcPersonalService;
    @RequiresPermissions("ruoyi:material:view")
    @GetMapping()
    public String material()
    {
        return prefix + "/material";
    }

    /**
     * 查询教材列表
     */
    @RequiresPermissions("ruoyi:material:listadmin")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(LhqTeachingMaterial lhqTeachingMaterial)
    {
        startPage();
        //ALL代表全部,如果搜索框内是全部就给传一个空值
        if (ALL.equals(lhqTeachingMaterial.getGrade())){
            lhqTeachingMaterial.setGrade(null);
            List<LhqTeachingMaterial> list = lhqTeachingMaterialService.selectLhqTeachingMaterialListGl(lhqTeachingMaterial);
            return getDataTable(list);
        }
        List<LhqTeachingMaterial> list = lhqTeachingMaterialService.selectLhqTeachingMaterialListGl(lhqTeachingMaterial);
        return getDataTable(list);
    }

    /**
     * 查询教材等级
     */
    @GetMapping("/grade")
    @ResponseBody
    public List<LhqTeachingMaterialGrade> list(LhqTeachingMaterialGrade lhqTeachingMaterialGrade)
    {
        return iLhqTeachingMaterialGradeService.selectLhqTeachingMaterialGradeList(lhqTeachingMaterialGrade);
    }

    /**
     * 导出教材列表
     */
    @RequiresPermissions("ruoyi:material:export")
    @Log(title = "教材", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(LhqTeachingMaterial lhqTeachingMaterial)
    {
        List<LhqTeachingMaterial> list = lhqTeachingMaterialService.selectLhqTeachingMaterialList(lhqTeachingMaterial);
        ExcelUtil<LhqTeachingMaterial> util = new ExcelUtil<LhqTeachingMaterial>(LhqTeachingMaterial.class);
        return util.exportExcel(list, "material");
    }

    /**
     * 新增教材
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存教材
     */
    @RequiresPermissions("ruoyi:material:add")
    @Log(title = "教材", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(LhqTeachingMaterial lhqTeachingMaterial)
    {
        LhqTeachingMaterialGrade lhqTeachingMaterialGrade = iLhqTeachingMaterialGradeService.selctByName(lhqTeachingMaterial.getGrade());
        lhqTeachingMaterial.setApplicant(iYcPersonalService.selectYcPersonalById(ShiroUtils.getUserId()).getuName());
        lhqTeachingMaterial.setUid(iYcPersonalService.selectYcPersonalById(ShiroUtils.getUserId()).getuId());
        lhqTeachingMaterial.setNaturalEndowments(lhqTeachingMaterialGrade.getNaturalEndowments());
        lhqTeachingMaterial.setReward(lhqTeachingMaterialGrade.getReward());
        return toAjax(lhqTeachingMaterialService.insertLhqTeachingMaterial(lhqTeachingMaterial));
    }

    /**
     * 修改教材
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        LhqTeachingMaterial lhqTeachingMaterial = lhqTeachingMaterialService.selectLhqTeachingMaterialById(id);
        mmap.put("lhqTeachingMaterial", lhqTeachingMaterial);
        return prefix + "/edit";
    }

    /**
     * 修改保存教材
     */
    @RequiresPermissions("ruoyi:material:edit")
    @Log(title = "教材", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(LhqTeachingMaterial lhqTeachingMaterial)
    {
        LhqTeachingMaterialGrade lhqTeachingMaterialGrade = iLhqTeachingMaterialGradeService.selctByName(lhqTeachingMaterial.getGrade());
        lhqTeachingMaterial.setNaturalEndowments(lhqTeachingMaterialGrade.getNaturalEndowments());
        lhqTeachingMaterial.setReward(lhqTeachingMaterialGrade.getReward());
        return toAjax(lhqTeachingMaterialService.updateLhqTeachingMaterial(lhqTeachingMaterial));
    }

    /**
     * 删除教材
     */
    @RequiresPermissions("ruoyi:material:remove")
    @Log(title = "教材", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(lhqTeachingMaterialService.deleteLhqTeachingMaterialByIds(ids));
    }
    /**
     * 通过
     */
    @RequiresPermissions("ruoyi:material:tongguo")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/tongguo")
    @ResponseBody
    public AjaxResult tongguo(Long id) {
        LhqTeachingMaterial lhqTeachingMaterial=new LhqTeachingMaterial();
        lhqTeachingMaterial.setId(id);
        lhqTeachingMaterial.setExamineApprove(3L);

        //更新审批状态为待审批
        lhqTeachingMaterialService.updateLhqTeachingMaterial(lhqTeachingMaterial);
        return success();
    }
    /**
     * 驳回
     */
    @RequiresPermissions("ruoyi:material:bohui")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/bohui")
    @ResponseBody
    public AjaxResult bohui(Long id) {
        LhqTeachingMaterial lhqTeachingMaterial=new LhqTeachingMaterial();
        lhqTeachingMaterial.setId(id);
        lhqTeachingMaterial.setExamineApprove(2L);
        //更新审批状态为待审批
        lhqTeachingMaterialService.updateLhqTeachingMaterial(lhqTeachingMaterial);
        return success();
    }
    /**
     * 备案驳回
     */
    @RequiresPermissions("ruoyi:material:beianbohui")
    @Log(title = "备案状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/beianbohui")
    @ResponseBody
    public AjaxResult beianbohui(Long id) {
        LhqTeachingMaterial lhqTeachingMaterial=new LhqTeachingMaterial();
        lhqTeachingMaterial.setId(id);
        lhqTeachingMaterial.setKeepOnRecord(3L);
        //更新审批状态为待审批
        lhqTeachingMaterialService.updateLhqTeachingMaterial(lhqTeachingMaterial);
        return success();
    }
    /**
     * 备案
     */
    @RequiresPermissions("ruoyi:material:beian")
    @Log(title = "审批状态", businessType = BusinessType.UPDATE)
    @PostMapping( "/beian")
    @ResponseBody
    public AjaxResult beian(Long id) {
        LhqTeachingMaterial lhqTeachingMaterial=new LhqTeachingMaterial();
        lhqTeachingMaterial.setId(id);
        lhqTeachingMaterial.setKeepOnRecord(1L);
        //更新审批状态为待审批
        lhqTeachingMaterialService.updateLhqTeachingMaterial(lhqTeachingMaterial);
        return success();
    }
    /**
     *
     */
    @RequiresPermissions("system:dict:list")
    @GetMapping("/chakan/{uid}")
    public String chakan(@PathVariable("uid") Long uid, ModelMap mmap)
    {

        YcPersonal ycPersonal = iYcPersonalService.selectYcPersonalById(uid);
        System.out.println(ycPersonal);
        mmap.put("ycPersonal", ycPersonal);
        return prefix + "/particulars";
    }
}
